<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="330" destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" ref="dialogForm" label-width="80px" label-position="left">
			<el-form-item label="字典类型" prop="type">
				<el-input v-model="form.type" clearable placeholder="字典类型"></el-input>
			</el-form-item>
			<el-form-item label="字典名称" prop="title">
				<el-input v-model="form.title" clearable placeholder="字典显示名称"></el-input>
			</el-form-item>
			<el-form-item label="备注" prop="description">
				<el-input v-model="form.description" clearable placeholder="备注"></el-input>
			</el-form-item>
			<el-form-item label="排序" prop="sortOrder">
				<el-input-number v-model="form.sortOrder" :min="0" :max="100" :precision="2" :step="1"   />
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible=false" >取 消</el-button>
			<el-button type="primary" :loading="isSaving" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
</template>

<script>
	export default {
		emits: ['success', 'closed'],
		data() {
			return {
				mode: "add",
				titleMap: {
					add: '新增字典',
					edit: '编辑字典'
				},
				visible: false,
				isSaving: false,
				form: {
					id:"",
					title: "",
					type: "",
					description: "",
					sortOrder:0
				},
				rules: {
					type: [
						{required: true, message: '请输入字典类型'}
					],
					title: [
						{required: true, message: '请输入字典名称'}
					],
					sortOrder: [
						{required: true, message: '请输入排序'}
					]
				},
				dic: [],
				dicProps: {
					value: "id",
					label: "title",
					emitPath: false,
					checkStrictly: true
				}
			}
		},
		mounted() {
			//this.getDic()
		},
		methods: {
			//显示
			open(mode='add'){
				this.mode = mode;
				this.visible = true;
				return this;
			},
			//获取字典列表
			async getDic(){
				var res = await this.$API.system.dic.tree.get();
				this.dic = res.data;
			},
			//表单提交方法
			submit(){
				this.$refs.dialogForm.validate(async (valid) => {
					if (valid) {
						this.isSaving = true;
						var res;
						if(this.mode == 'add')res = await this.$API.system.dict.save.post(this.form);
						else if(this.mode == 'edit') res = await this.$API.system.dict.edit.post(this.form);
						this.isSaving = false;
						if(res.success){
							this.$emit('success', this.form, this.mode)
							this.visible = false;
							this.$message.success("操作成功")
						}
					}
				})
			},
			//表单注入数据
			setData(data){
				this.form.id = data.id
				this.form.type = data.type
				this.form.title = data.title
				this.form.description = data.description
				this.form.sortOrder = data.sortOrder
			}
		}
	}
</script>

<style>
</style>
